package com.example.yin.service.impl;

import com.example.yin.dao.ListSongMapper;
import com.example.yin.dao.RankSongMapper;
import com.example.yin.dao.SongListMapper;
import com.example.yin.dao.SongMapper;
import com.example.yin.domain.ListSong;
import com.example.yin.domain.RankSong;
import com.example.yin.domain.Song;
import com.example.yin.domain.SongList;
import com.example.yin.service.SongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class SongServiceImpl implements SongService {

    @Autowired
    private  SongMapper songMapper;
    @Autowired
    private SongListMapper songListMapper;
    @Autowired
    private ListSongMapper listSongMapper;
    @Autowired
    private RankSongMapper rankSongMapper;
    @Override
    public List<Song> queryBySongname(String str) {
        return songMapper.queryBySongname(str);
    }//根据歌单名字和歌手名字模糊查询

    @Override
    public List<SongList> queryByListname(String str) {
        return songListMapper.queryByListname(str);
    }//根据歌单名字和歌单风格模糊查询

    @Override
    public List<Song> allSongs(Integer id) {
        // List<Integer> songsId = listSongMapper.allSongs(id);
        List<ListSong> listSongs = listSongMapper.allSongs(id);
        List<Song> songs = new LinkedList<>();
        for(int i = 0;i<listSongs.size();i++){
            songs.add(songMapper.selectById(listSongs.get(i).getSongId()));
        }
        return songs;
    }//返回歌单下所有歌曲

    @Override
    public Song getSongById(Integer id) {
        return songMapper.selectById(id);
    }//根据歌曲id返回歌曲信息

    @Override
    public boolean scoreSong(RankSong rankSong) {
        rankSongMapper.insert(rankSong);
        List<RankSong> scores= rankSongMapper.selectScoreBySongId(rankSong.getSongId());
        Song song = songMapper.selectById(rankSong.getSongId());
        song.setEvaluators(scores.size());
        float sc = 0;
        for(int i = 0;i<scores.size();i++){
            sc =sc+scores.get(i).getScore();
        }
        sc=sc/scores.size();
        song.setScore(sc);
        return songMapper.updateByPrimaryKey(song)>0;
    }




    /*admin module 管理员增删改查用户数据*/
    @Override
    public List<Song> allSong()
    {
        return songMapper.allSong();
    }

    @Override
    public boolean addSong(Song song)
    {

        return songMapper.addSong(song) > 0?true:false;
    }

    @Override
    public boolean deleteSong(Integer id) {
        return songMapper.deleteSong(id) >0 ?true:false;
    }

//    @Override
//    public boolean updateByPrimaryKey(Song song) {
//        return songMapper.updateByPrimaryKey(song)>0;
//    }

    @Override
    public List<Song> songOfSingerName(String name) {
        return songMapper.songOfSingerName(name);
    }

    @Override
    public List<Song> songOfName(String name) {
        return songMapper.songOfName(name);
    }

    @Override
    public List<Song> songOfSingerId(Integer singerId)

    {
        return songMapper.songOfSingerId(singerId);
    }

    @Override
    public boolean updateSongMsg(Song song) {
        return songMapper.updateSongMsg(song) >0 ?true:false;
    }

    @Override
    public boolean updateSongUrl(Song song) {

        return songMapper.updateSongUrl(song) >0 ?true:false;
    }

    @Override
    public boolean updateSongPic(Song song) {

        return songMapper.updateSongPic(song) >0 ?true:false;
    }

}
